<?php
require_once 'protoweb/ofertas_protoweb.php';

/**
 * @package sisco
 * @subpackage controller
 */
class Ofertas extends Ofertas_protoweb
{
    public function create()
    {
        // chequeo y obtengo el usuario loggeado
        $data['logueado'] = $this->getUsuarioLogueado();

        // valido permisos
        $gruposValidos = array();
        // ej. array('venta','logistica','contable','directivo');
        $this->validarPermisos($data['logueado'], $gruposValidos);

        // Get an empty object
        $data['oferta'] = new Oferta();

        // Message Handler
        $data['msg'] = new PwMsgHandler();
        
        // Seleccionar secciones
        $data['navbar_precios'] = 'active';
        $data['sidebar_ofertas_create'] = 'active';
        
        // prepare object attributes
        $this->createPrepare($data);

        // if posting, try to save the new object
        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            $this->create_save($data);
        }

        // Pre Load
        $data['categorias'] = new Categoria();
        $data['categorias']->get();
        $data['marcas'] = new Marca();
        $data['marcas']->get();
        
        // load view
        $this->smartyview->render('oferta_create.html', $data);

    } // function create

    private function create_save($data) 
    {
        // assignCreatePost is defined on parent class and 
        // will assign all posting values to your attributes's class 
        // based on purposes definitions
        if ($this->assignCreatePost($data)) {

            // You could customize attributes here

            // Saving Object (feel free to edit)
            if ($this->saveEntity($data)) { 
            
                // post processing object attributes
                $this->afterCreateSuccess($data);

                // Show success message in next view
                $data['msg']->success = 'Oferta creada correctamente';
                $data['msg']->persistSuccess();

                // redirects to update method
                redirect(base_url() . 'ofertas/update/' . $data['oferta']->id . '/');
            }
            else { 
                
                // revert attributes changes if necesary
                $this->revertOnCreateError($data);

                // Append model's errors
                $data['msg']->error .= $data['oferta']->error->string;
            }
        } // if assignCreatePost
    } // function create_save
    
    public function update($id)
    {
        // chequeo y obtengo el usuario loggeado
        $data['logueado'] = $this->getUsuarioLogueado();

        // valido permisos
        $gruposValidos = array();
        // ej. array('venta','logistica','contable','directivo');
        $this->validarPermisos($data['logueado'], $gruposValidos);

        // get object to edit
        $data['oferta'] = new Oferta($id);
        $data['categorias'] = new Categoria();
        $data['categorias']->get();
        $data['marcas'] = new Marca();
        $data['marcas']->get();

        // Message Handler
        $data['msg'] = new PwMsgHandler();
        
        // Seleccionar secciones
        $data['navbar_precios'] = 'active';
        $data['sidebar_ofertas_browse'] = 'active';
        
        // prepare object attributes
        $this->updatePrepare($data);

        // if posting, try to save modifications
        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            $this->update_save($data);
        }

        // load view
        $this->smartyview->render('oferta_update.html', $data);

    } // function update

    private function update_save($data) 
    {
        // assignUpdatePost is defined on parent class and 
        // will assign all posting values to your attributes's class 
        // based on purposes definitions
        if ($this->assignUpdatePost($data)) {

            // You could customize attributes here

            // Saving Object and its relationships (feel free to edit)
            if ($this->saveEntity($data)) { 
            
                // post processing object attributes
                $this->afterUpdateSuccess($data);

                // Show success message in next view
                $data['msg']->success = 'Oferta actualizada correctamente';
                $data['msg']->persistSuccess();

                // redirects to edit
                redirect(base_url() . 'ofertas/update/' . $data['oferta']->id . '/');
            }
            else { 
                // revert attributes changes if necesary
                $this->revertOnUpdateError($data);

                // Append model's errors
                $data['msg']->error .= $data['oferta']->error->string;
            }
        } // if assignUpdatePost
    } // function update_save
    
    public function delete($id)
    {
        // chequeo y obtengo el usuario loggeado
        $data['logueado'] = $this->getUsuarioLogueado();

        // valido permisos
        $gruposValidos = array();
        // ej. array('venta','logistica','contable','directivo');
        $this->validarPermisos($data['logueado'], $gruposValidos);

        // get object to delete
        $data['oferta'] = new Oferta($id);
        
        if ($data['oferta']->delete()) {

            // delete related actions based on purpose
            $this->afterDeleteSuccess($data);

            // Show success message in next view
            $data['msg']          = new PwMsgHandler();
            $data['msg']->success = 'oferta successfully deleted';
            $data['msg']->persistSuccess();

            // redirects to create a new object
            redirect(base_url() . 'ofertas/create/');
        }
    } // function delete

    public function browse() {
        // chequeo y obtengo el usuario loggeado
        $data['logueado'] = $this->getUsuarioLogueado();

        // valido permisos
        $gruposValidos = array();
        // ej. array('venta','logistica','contable','directivo');
        $this->validarPermisos($data['logueado'], $gruposValidos);

        // Message Handler
        $data['msg'] = new PwMsgHandler();

        // Seleccionar secciones
        $data['navbar_precios'] 		= 'active';
        $data['sidebar_ofertas_browse'] = 'active';
        
        // get objects to show
        $data['ofertas'] = new Oferta();
        $data['ofertas']->get();
        $data['marcas'] = new Marca();
        $data['categorias'] = new Categoria();
        $data['productos'] = new Producto();
        
        $this->smartyview->render('oferta_browse.html',$data);
    } // function browse
    
	public function __construct() 
	{
		parent::__construct();
	}

} // class Ofertas

/* End of file ofertas.php */
/* Location: ./application/controllers/ofertas.php */